package com.amumu.drama.admin.modules.bak.service.impl;

import com.amumu.drama.admin.dto.InvitationDTO;
import com.amumu.drama.mapper.InvitationMapper;
import com.amumu.drama.mapper.UmsMemberMapper;
import com.amumu.drama.model.Invitation;
import com.amumu.drama.model.InvitationExample;
import com.amumu.drama.model.UmsMember;
import com.amumu.drama.admin.modules.bak.service.SmsInvitationService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SmsInvitationServiceImpl implements SmsInvitationService {

    @Autowired
    private InvitationMapper invitationMapper;
    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Override
    public List<InvitationDTO> search(String invitationCode, String phoneNum, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNotBlank(invitationCode)) {
            Invitation invitation = invitationMapper.findByCode(invitationCode);
            if (invitation == null) {
                return null;
            }
            UmsMember member = umsMemberMapper.selectByPrimaryKey(invitation.getId());
            if (member == null) {
                return null;
            }

            return this.convert2Dto(Collections.singletonList(invitation));
        }

        Long memberId = null;
        if (StringUtils.isNotBlank(phoneNum)) {
            UmsMember member = umsMemberMapper.findByPhoneNum(phoneNum);
            if (member == null) {
                return null;
            }
            memberId = member.getId();
        }

        PageHelper.startPage(pageNum, pageSize);
        InvitationExample example = new InvitationExample();
        InvitationExample.Criteria criteria = example.createCriteria();
        if (memberId != null) {
            criteria.andMemberIdEqualTo(memberId);
        }
        example.setOrderByClause("id desc");
        List<Invitation> invitationList = invitationMapper.selectByExample(example);
        return this.convert2Dto(invitationList);
    }

    private List<InvitationDTO> convert2Dto(List<Invitation> invitationList) {
        List<InvitationDTO> invitationDTOList = new ArrayList<>(invitationList.size());
        for (Invitation invitation : invitationList) {
            InvitationDTO invitationDTO = new InvitationDTO();
            invitationDTO.setId(invitation.getId());
            invitationDTO.setDiscount(invitation.getDiscount());
            invitationDTO.setEnabled(invitation.getStatus() == 1);
            invitationDTO.setInvitationCode(invitation.getCode());
            invitationDTO.setGmtCreate(invitation.getGmtCreate());
            invitationDTO.setStartTime(invitation.getStartTime());
            invitationDTO.setEndTime(invitation.getEndTime());

            LocalDateTime now = LocalDateTime.now();
            if (invitation.getStartTime() == null || invitation.getEndTime() == null) {
                invitationDTO.setStatus("无效");
            } else if (invitation.getStartTime().isAfter(now)) {
                invitationDTO.setStatus("未生效");
            } else if (invitation.getEndTime().isBefore(now)) {
                invitationDTO.setStatus("已过期");
            } else {
                invitationDTO.setStatus("有效");
            }

            UmsMember member = umsMemberMapper.selectByPrimaryKey(invitation.getMemberId());
            if (member != null) {
                invitationDTO.setMemberName(member.getUsername());
                invitationDTO.setPhoneNum(member.getPhone());
            }
            invitationDTOList.add(invitationDTO);
        }
        return invitationDTOList;
    }
}
